package me.arvin.qa.jmeter.plugins.dependscontroller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.jmeter.samplers.SampleResult;

class DependsRelationMap {
	private Map<String, Map<String, SampleResult>> dependsOnMap = new HashMap<String, Map<String, SampleResult>>();

	/**
	 * 根据被依赖的关系建立的映射 可通过被依赖的组件id查找依赖项
	 */
	private Map<String, Set<String>> beDependsOnMap = new HashMap<String, Set<String>>();

	Map<String, Map<String, SampleResult>> getDependsOnMap() {
		return dependsOnMap;
	}

	Map<String, Set<String>> getBeDependsOnMap() {
		return beDependsOnMap;
	}

	/**
	 * @param id
	 *            依赖者的id
	 * @param beDependsId
	 *            被依赖者的id
	 */
	void addDependsReleation(String id, String beDependsId) {
		dependsOnMap.computeIfAbsent(id, v -> new HashMap<String, SampleResult>()).put(beDependsId, null);
		beDependsOnMap.computeIfAbsent(beDependsId, v -> new HashSet<String>()).add(id);
	}

	void removeDependsRelation(String id) {
		dependsOnMap.remove(id);
		beDependsOnMap.forEach((k, v) -> v.removeIf(id::equals));
	}

	void clear() {
		dependsOnMap.clear();
		beDependsOnMap.clear();
	}

	@Override
	public String toString() {

		return "dependsOnMap:" + dependsOnMap + "\nbeDependsOnMap:" + beDependsOnMap;
	}
}